<?php
namespace app\common\customer\model;


use app\common\config\logic\DictionaryLogic;
use app\common\customer\logic\ContactLogic;
use app\common\system\logic\UserLogic;
use think\Model;
use think\model\concern\SoftDelete;
use app\common\config\model\ConfigStatus;
use util\Format;

class CustomerAllocate extends Model
{
    protected $table = "tk_member_allocate";

    use SoftDelete;
    protected $defaultSoftDelete = 0;
    protected $type = [
        "next_visit_time" => "timestamp",
        "sign_time" => "timestamp"
    ];

    public function member()
    {
        return $this->hasOne(Customer::class, "id", "member_id");
    }

    public function getContactAttr($value, $data)
    {
        $contactLogic = new ContactLogic();
        $row = $contactLogic->getContactDefault($data['member_id'], ['id', 'role_type', 'contact_type', 'tag', 'contact']);
        // $row['contact'] = Format::mobile($row['contact']);

        return $row;
    }

    // 获取创建者
    public function getCreatorAttr($value, $data) {
        $logic = new UserLogic();

        return $logic->getUser($data['create_id'], ['id', 'realname', 'status']);
    }

    public function getAssignTypeAttr($value, $data) {
        $text = "";
        switch ($value) {
            case "1":
                $text = "创建获取";
                break;
            case "2":
                $text = "自动分配";
                break;
            case "3":
                $text = "手动分配";
                break;
            case "4":
                $text = "全号获取";
                break;
        }

        return $text;
    }

    // 分配人
    public function getFromUserAttr($value, $data){
        $logic = new UserLogic();

        return $logic->getUser($data['from_user_id'], ['id', 'realname', 'status']);
    }

    // 当前任务的跟踪者
    public function getUserAttr($value, $data) {
        $logic = new UserLogic();

        return $logic->getUser($data['user_id'], ['id', 'realname', 'status']);
    }

    public function getVisitStatusAttr($value, $data) {
        $dictionaryLogic = new DictionaryLogic();
        $row = $dictionaryLogic->getItem($data['status'], ConfigStatus::class, ['id','title']);
        if (empty($row)) {
            $row = [
                'id' => 0,
                'title' => '待跟进'
            ];
        }

        return $row;
    }

    public function getLastVisitAttr($value, $data) {
        $where = [];
        $where[] = ['user_id', '=', $data['user_id']];
        $where[] = ['member_id', '=', $data['member_id']];

        $model = new CustomerVisit();
        return $model->where($where)->order('id desc')->find();
    }

    public function getProductAttr($value, $data) {

        $where = [];
        $where[] = ['member_id', '=', $data['member_id']];

        $model = new CustomerMami();
        $row = $model->where($where)->visible(['id', 'member_id', 'store_id','event_date', 'entry_time', 'event_month', 'budget', 'remark'])->find();
        if (empty($row)) {
            $row = [
                'id' => 0,
                'member_id' => 0,
                'event_date' => 0,
                'budget' => 0,
                'remark' => '',
                'store' => [
                    'id' => '',
                    'title' => ''
                ],
                'category' => [
                    'id' => '',
                    'title' => ''
                ]
            ];
        }
        $row->append(['store']);
        return $row;
    }
}